package 线程池;

import java.util.concurrent.*;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/8/16 15:14
 * @description:
 * @version:
 */
public class SubmitVsExecuteExample {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5, 10, 5000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100));

        // 使用 execute() 方法提交任务
        try {
            threadPoolExecutor.execute(new RunnableTask());
        }catch (Exception e) {
            System.out.println("execute 过程出现异常");
        }


        // 使用 submit() 方法提交任务
        Future<String> future = threadPoolExecutor.submit(new CallableTask());
        try {
            String result = future.get();
            System.out.println("Task result: " + result);
        } catch (Exception e) {
            System.out.println("submit过程发生异常");
            //e.printStackTrace();
        }

        threadPoolExecutor.shutdown();
    }
}

class RunnableTask implements Runnable {
    @Override
    public void run() {
        try {
            Integer oiss = Integer.valueOf("oiss");
            System.out.println(oiss);
            System.out.println("Executing RunnableTask");
        }catch (Exception e) {
            System.out.println(e);
        }
    }
}

class CallableTask implements Callable<String> {
    @Override
    public String call() throws Exception {
        Integer oiss = Integer.valueOf("oiss");
        System.out.println(oiss);
        System.out.println("Executing CallableTask");
        return "Task Result";
    }
}

class CallableTask2 implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        return null;
    }
}
